package model.dao.registerOrder;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import log.MyLogger;
import model.service.ServiceDevice;
import model.service.ServiceOrder;


public class ServiceDeviceDAOImpl{
	String sql = "" ;
	db.dbconnection dbconnection ;
	Connection myConnection ;
	Statement st ;
	
	public List<ServiceDevice> getServiceDevicesByServiceOrder(int serviceOrder) {
		
		List<ServiceDevice> serviceDevices = new ArrayList<ServiceDevice>();
		if(serviceOrder <= 0)
			return serviceDevices;
		
		try {
			dbconnection = new db.dbconnection();
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql = "SELECT * FROM service_device WHERE service_order_fk = "+serviceOrder;
			ResultSet resultSet = st.executeQuery(sql);
			DeviceDAOImpl deviceDAOImpl = new DeviceDAOImpl();
			ServiceDeviceStatusTypeDAOImpl serviceDeviceStatusTypeDAOImpl = new ServiceDeviceStatusTypeDAOImpl();

			
			while(resultSet.next()){
				ServiceDevice serviceDevice = new ServiceDevice();
				serviceDevice.setServiceDescription(resultSet.getString("service_description"));
				serviceDevice.setServiceDevice(resultSet.getInt("service_device"));
				serviceDevice.setDevice(deviceDAOImpl.getDeviceById(resultSet.getInt("device_fk")));
				serviceDevice.setServiceDeviceStatusType(serviceDeviceStatusTypeDAOImpl.getServiceDeviceStatusTypeById(resultSet.getInt("service_device_status_type_fk")));
				serviceDevice.setServiceOrder(new ServiceOrder(resultSet.getInt("service_order_fk")));
				serviceDevice.setToStore(resultSet.getTimestamp("to_store"));
				serviceDevice.setFromStore(resultSet.getTimestamp("from_store"));
				serviceDevice.setStatusChanged(resultSet.getTimestamp("status_changed"));
				serviceDevice.setStoreStatus(resultSet.getInt("store_status"));
				serviceDevices.add(serviceDevice);
			}
			
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceDeviceDAOImpl.getServiceDevices()", e.getMessage());
		}
	

		return serviceDevices;
	}

	public void insertServiceDevice(ServiceDevice serviceDevice) {
		try {
			dbconnection = new db.dbconnection();
			myConnection= dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			
			String sql = null;
			
			sql = "INSERT INTO service_device (device_fk, service_order_fk) VALUES (" + serviceDevice.getDevice().getDevice() + ", " + serviceDevice.getServiceOrder().getServiceOrder() + ")";

			st.executeQuery(sql);
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceDeviceDAOImpl.insertServiceDevice()", e.getMessage());
		}
		
	}

	public void deleteServiceDevice(int service_device) {
		try {
			myConnection= dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			
			String sql = "delete from service_device WHERE service_device = "+service_device;
			st.executeUpdate(sql);
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceDeviceDAOImpl.deleteServiceDevice()", e.getMessage());
		}
		
	}
	
	public ServiceDevice getServiceDeviceById(int serviceOrderId){
		ServiceDevice serviceDevice = null;
		try {
			Connection c = dbconnection.getConnection();
			Statement st = c.createStatement();
			String sql = "SELECT * FROM service_device WHERE service_device = "+serviceOrderId;
			ResultSet resultSet = st.executeQuery(sql);
			
			DeviceDAOImpl deviceDAOImpl = new DeviceDAOImpl();
			ServiceDeviceStatusTypeDAOImpl serviceDeviceStatusTypeDAOImpl = new ServiceDeviceStatusTypeDAOImpl();
			ServiceOrderDAOImpl serviceOrderDAOImpl=new ServiceOrderDAOImpl();
			
			
			while(resultSet.next()){
				ServiceDevice s = new ServiceDevice();
				
				s.setServiceDescription(resultSet.getString("service_description"));
				s.setServiceDevice(resultSet.getInt("service_device"));
				s.setDevice(deviceDAOImpl.getDeviceById(resultSet.getInt("device_fk")));
				s.setServiceDeviceStatusType(serviceDeviceStatusTypeDAOImpl.getServiceDeviceStatusTypeById(resultSet.getInt("service_device_status_type_fk")));
				s.setServiceOrder(serviceOrderDAOImpl.getServiceOrderById(resultSet.getInt("service_order_fk")));
				s.setToStore(resultSet.getTimestamp("to_store"));
				s.setFromStore(resultSet.getTimestamp("from_store"));
				s.setStatusChanged(resultSet.getTimestamp("status_changed"));
				s.setStoreStatus(resultSet.getInt("store_status"));
				
				serviceDevice = s;
			}
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceDeviceDAOImpl.getServiceDeviceById()", e.getMessage());
			return null;
		}
		return serviceDevice;
	}


	public boolean updateServicedevice(ServiceDevice serviceDevice) {
		if(serviceDevice == null)
			return false;
		try {
			dbconnection = new db.dbconnection();
			myConnection = dbconnection.getConnection();
			Statement st =myConnection.createStatement();
			String sql = "UPDATE service_device SET device_fk = " + serviceDevice.getDevice().getDevice() + " WHERE service_device = " + serviceDevice.getServiceDevice() + "";
			st.executeUpdate(sql);
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceDeviceDAOImpl.updateServicedevice()", e.getMessage());
			return false;
		}
		
		return false;
	}

}
